from PyQt5 import QtCore,QtSql
from PyQt5.QtWidgets import QDialog, QLabel, QLineEdit, QPushButton, QHBoxLayout, QVBoxLayout, QMessageBox, QWidget, \
    QComboBox
from PyQt5.QtSql import QSqlQuery
from controller.user_controller import select_user, register_verify, register
from dao.user_dao import select_user_by_username
from dao.database import new_user
from models.user import User


class SigninPage(QWidget):
    print("select_user_by_username(username)", select_user_by_username("123"))

    switch_loginin_window = QtCore.pyqtSignal()

    def __init__(self):
        super(SigninPage, self).__init__()

        self.setWindowTitle('注册Hello Word')
        self.signin_user_label = QLabel('用户名:', self)
        self.signin_email_label = QLabel('邮箱:', self)
        self.signin_pwd_label = QLabel('密码:', self)
        self.signin_pwd2_label = QLabel('确认密码:', self)

        self.signin_user_line = QLineEdit(self)
        self.signin_email_line = QLineEdit(self)
        self.signin_pwd_line = QLineEdit(self)
        self.signin_pwd2_line = QLineEdit(self)
        self.signin_exam_combobox = QComboBox(self)  # 选择考试类型下拉框
        self.signin_button = QPushButton('Sign in', self)

        self.user_h_layout = QHBoxLayout()
        self.email_h_layout = QHBoxLayout()
        self.pwd_h_layout = QHBoxLayout()
        self.pwd2_h_layout = QHBoxLayout()  # 水平布局QHBoxLayout 将控件从左到右依次水平摆放
        self.all_v_layout = QVBoxLayout()  # 垂直布局QVBoxLayout

        self.lineedit_init()
        self.combobox_init()
        self.pushbutton_init()
        self.layout_init()

    def layout_init(self):
        self.user_h_layout.addWidget(self.signin_user_label)
        self.user_h_layout.addWidget(self.signin_user_line)
        self.email_h_layout.addWidget(self.signin_email_label)
        self.email_h_layout.addWidget(self.signin_email_line)
        self.pwd_h_layout.addWidget(self.signin_pwd_label)
        self.pwd_h_layout.addWidget(self.signin_pwd_line)
        self.pwd2_h_layout.addWidget(self.signin_pwd2_label)
        self.pwd2_h_layout.addWidget(self.signin_pwd2_line)

        self.all_v_layout.addLayout(self.user_h_layout)
        self.all_v_layout.addLayout(self.email_h_layout)
        self.all_v_layout.addLayout(self.pwd_h_layout)
        self.all_v_layout.addLayout(self.pwd2_h_layout)
        self.all_v_layout.addWidget(self.signin_exam_combobox)
        self.all_v_layout.addWidget(self.signin_button)

        self.setLayout(self.all_v_layout)

    def lineedit_init(self):
        self.signin_pwd_line.setEchoMode(QLineEdit.EchoMode.Password)
        self.signin_pwd2_line.setEchoMode(QLineEdit.EchoMode.Password)

        self.signin_user_line.textChanged.connect(self.check_input_func)
        self.signin_pwd_line.textChanged.connect(self.check_input_func)
        self.signin_pwd2_line.textChanged.connect(self.check_input_func)

    def combobox_init(self):
        self.signin_exam_combobox.addItem("小升初")
        self.signin_exam_combobox.addItem("中考")
        self.signin_exam_combobox.addItem("高考")
        self.signin_exam_combobox.addItem("四六级")
        self.signin_exam_combobox.addItem("考研")

    def pushbutton_init(self):
        self.signin_button.setEnabled(False)

        self.signin_button.clicked.connect(self.check_signin_func)

    def check_input_func(self):
        if self.signin_user_line.text() and self.signin_pwd_line.text() and self.signin_pwd2_line.text():
            self.signin_button.setEnabled(True)
        else:
            self.signin_button.setEnabled(False)

    def check_signin_func(self):
        username = self.signin_user_line.text()
        email = self.signin_email_line.text()
        password = self.signin_pwd_line.text()
        password2 = self.signin_pwd2_line.text()
        exam = self.signin_exam_combobox.currentIndex()
#        print("select_user_by_username(username)",self.select_user_by_username("123"))

        # 验证两次输入的密码
        if password != password2:
            QMessageBox.critical(self, '错误', '两次输入不一致!')
            self.signin_pwd_line.clear()
            self.signin_pwd2_line.clear()
        elif "@" not in self.signin_email_line.text():
            QMessageBox.critical(self, '错误', '请输入正确的邮箱!')
            self.signin_email_line.clear()
        # elif self.select_user_by_username(username):
        #     QMessageBox.critical(self, '错误', '用户名已被注册!')
        #     self.signin_user_line.clear()
        else:
            new_user(username, password, email, exam)
            QMessageBox.information(self, 'Information', '注册成功')
            self.switch_loginin_window.emit()


if __name__ == '__main__':
    pass
